// index page
// ============

define(['require', 'zepto', 'utils', 'wx', 'ejs', 'sui'], function(require, $, utils, wx, ejs) {
  return {
    init: function(data) {
      this.unionid=data.unionid
      utils.configWeixin(data.jsConfig, false);
      this.requestData(data);
      this.headerHtml = 
         '<% products.forEach(function(item, index) { %>\
          <li>\
          <a href="'+window.__CTX__.root+'/user-courses/<%=coursesMap[item.id].id%>" class="item-content item-link shopping-content">\
          <% if(item.id != 1) {%>\
          <div class="item-media" style="<%= item.single_avatar == true ? "width: 6rem; display: block;" : "" %>">\
            <% if(item.single_avatar == false) {%>\
            <img src="'+window.__CTX__.resource+'<%=item.coach_avatar%>.jpg?v=1.1.1" style="width: 6rem;">\
            <% } else { %>\
             <% item.coach_avatar_list.forEach(function(imageItem){%>\
               <img src="'+window.__CTX__.resource+'<%=imageItem%>.jpg?v=1.1.1" style="width: 6rem; margin-bottom: .3rem;">\
             <% }) %>\
             <% } %>\
          </div>\
          <% } %>\
      <div class="item-inner <%=item.id == 1 ? "no-pic" : "" %>" style="margin-left: .5rem;">\
        <div class="item-title-row">\
          <div class="item-title"><%=item.name%></div>\
        </div>\
        <div class="item-subtitle">\
        <div class="">课程导师：\
        <% item.coach_name_list.forEach(function(name, index){%>\
        <%= index == 0 ? "" : "&" %> <%=name%> \
        <% }) %>\
        </div>\
        <div class="">上课时间：<%=item.occur_time%></div>\
        </div>\
        <div class="item-text" style="height: 3rem;">\
        <div><%=item.id == 1 ? "精英" : "" %>装备包</div>\
        <div><%=item.equipment_package%></div>\
      </div>\
      </div>\
  </a></li>\
        <% }) %>';
      
      this.emptyHtml = '<h1 style="font-size: 1.4rem;text-align: center;font-weight: bold; margin-top: 5rem; color: #000;">您还未购买任何课程</h1>\
                       <p style="text-align: center; font-size: .7rem; margin: .3rem 0; color: #000;">请返回购买</p>';
      
    },
    
    requestData: function(data) {
      var that = this;
      $.ajax({
        type : 'GET',
        url : window.__CTX__.domain + '/api/profile/courses?unionid=' + that.unionid,
        dataType : 'json',
        timeout : 30000,
        success : function(res) {
          console.log(res);
          if(res.code == 0) {
              that.products = res.data.products
              that.coursesMap = res.data.coursesMap
              that.courses = res.data.courses
              that.courseLength = res.data.courseLength
              if(that.courseLength > 0) {
                var headerResult = ejs.render(that.headerHtml, {products : that.products, coursesMap: that.coursesMap})
                $('.course-list').html(headerResult);
              } else {
                $('.course-list').html(that.emptyHtml);
              }
              
          } else {
            $.toast("获取我的课程数据失败");
          } 
        },
        error : function(xhr, type) {
          alert('Ajax error!')
        }
      })
    }
  }
})